﻿using Awsc.DataAcquisitionSys.Core.Model.Config;
using Awsc.DataAcquisitionSys.Core.Models.ServiceModels;
using Awsc.DataAcquisitionSys.Core.Services.DataAcquisition.DataCollectors;

namespace Awsc.DataAcquisitionSys.Core.Service.DataAcquisition.DataCollectors
{
    public class HydraulicTongsCollector : TcpDataCollector
    {
        const ushort startAddress = 0;    //起始地址
        const ushort numberOfPoints = 15; //读数据长度
        byte[] buffer = new byte[100];
        public HydraulicTongsCollector(DataCollectionContext dataCollectionContext) : base(dataCollectionContext)
        {
        }

        protected async override Task CollectDataInternalAsync()
        {
            var readedBytes = TcpStream.Read(buffer, 0, buffer.Length);
            if (readedBytes >= numberOfPoints)
            {
                var sensorData = TypeConvert(buffer[3], buffer[4]);
                SetAISensorValue(1, sensorData);
                await TcpStream.FlushAsync();
            }
            else if (readedBytes >= 4)
            {
                var sensorData = TypeConvert(buffer[2], buffer[3]);
                SetAISensorValue(1, sensorData);
                await TcpStream.FlushAsync();
            }
        }
        private float TypeConvert(byte P1, byte P2)
        {
            return (float)(P1 << 8 | P2);
        }
    }
}
